const api = require('../../utils/utils')
var app = getApp()
Page({
  data: {
    iconUrl: api.iconUrl,
    patient_id: '', // 记录本页面的患者id
    labels: [], // 患者标签列表
    label_id: '', // 标记被选中/删除的标签
    selectList: ['头部', '颈部', '胸部', '背部', '上肢', '下肢', '其他部位'],
    label_name: "", // 添加的标签
  },
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '标签列表'
    })
    // 将传递到页面的数据保留
    this.setData({
      patient_id: options.patient_id,
    })
    this.getLabels(options.patient_id)
  },
  // 将选中的标签传回
  returnBack(event) {
    const id = event.target.id
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('returnEvent', {
      label_id: id,
    });
    wx.navigateBack({
      delta: 1,
    })
  },
  // 获取疤痕标签列表
  getLabels(patient_id) {
    const url = `/labels/${patient_id}/patient/`
    api.request('get', url).then(res => {
      this.setData({
        labels: res.labels,
      })
    })
  },
  // 显示添加标签的对话框
  showAddDialog() {
    const dialog = this.selectComponent("#addDialog");
    dialog.linShow({
      type: "confirm",
      title: "添加标签"
    })
  },
  // 清空标签字段
  resetFields() {
    this.setData({
      label_name: ''
    })
  },
  // 提示是否删除标签
  showDeleteDialog(event) {
    this.setData({
      label_id: event.target.id
    })
    const dialog = this.selectComponent('#dialog');
    dialog.linShow({
      title: '确认删除？',
      type: 'confirm'
    });
  },
  // 请求删除标签
  deleteRequest() {
    const instance = this.data.labels.find((e) => {
      return e.id == this.data.label_id
    })
    if (instance.records.length !== 0) {
      const toast = this.selectComponent("#toast")
      toast.linShow({
        title: "该标签下仍有记录，无法删除",
        icon: "warning"
      })
      return 
    }
    api.request('delete', `/labels/${this.data.patient_id}/patient/${this.data.label_id}/`)
      .then(res => {
        const toast = this.selectComponent('#toast');
        toast.linShow({
          title: '删除成功',
          icon: 'success'
        });
        this.getLabels(this.data.patient_id)
      })
      .catch(err => {
        const toast = this.selectComponent('#toast');
        toast.linShow({
          title: '删除失败',
          icon: 'error'
        });
      })
  },
  // 获取输入标签
  label_input(e) {
    this.setData({
      label_name: e.detail.value
    })
  },
  // 选择列表监听函数
  pickerChange: function (e) {
    if (e.detail.value != this.data.selectList.length - 1) {
      this.setData({
        label_name: this.data.selectList[e.detail.value],
      })
    }
  },
  // 请求添加标签
  addRequest() {
    let index = this.data.labels.findIndex((e) => {
      return e.name == this.data.label_name
    })
    if (index !== -1) {
      const toast = this.selectComponent("#toast")
      toast.linShow({
        title: "该标签已存在，请勿重复添加",
        icon: "error"
      })
      this.setData({
        label_name: ''
      })
      return
    }
    let data = {
      name: this.data.label_name
    }
    api.request('post', `/labels/${this.data.patient_id}/patient/`, data)
      .then(res => {
        const toast = this.selectComponent("#toast")
        toast.linShow({
          title: '添加成功',
          icon: 'success'
        })
        this.getLabels(this.data.patient_id)
        this.setData({
          addrow: true
        })
      })
      .catch(err => {
        const toast = this.selectComponent("#toast")
        toast.linShow({
          title: '添加失败',
          icon: 'error'
        })
      })
  },
})